package com.pilipili.zeus.eduService.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pilipili.commonutils.JwtUtils;
import com.pilipili.commonutils.Result;
import com.pilipili.commonutils.vo.User;
import com.pilipili.zeus.eduService.client.UcenterClient;
import com.pilipili.zeus.eduService.entity.EduComment;
import com.pilipili.zeus.eduService.entity.comment.CommentNode;
import com.pilipili.zeus.eduService.service.EduCommentService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 评论 前端控制器
 *
 * @author Unlimited_Blade_Works
 * @since 2021-06-03
 */
@RestController
@RequestMapping("/eduService/comment")
public class EduCommentController {
    @Autowired
    private EduCommentService commentService;
    @Autowired
    private UcenterClient ucenterClient;
    @ApiOperation(value = "读取树形分类列表")
    @GetMapping("getComments")
    public Result getComments(){
        List<CommentNode> list=commentService.getCommentTree();
        return Result.ok().data("list",list);
    }
    //根据课程id查询评论列表
    @ApiOperation(value = "评论分页列表")
    @GetMapping("{page}/{limit}")
    public Result index(@ApiParam(name = "page", value = "当前页码", required = true)
                        @PathVariable Long page,
                        @ApiParam(name = "limit", value = "每页记录数", required = true)
                        @PathVariable Long limit,
                        @ApiParam(name = "courseId", value = "查询对象")
                                    String courseId) {
        Page<EduComment> pageParam = new Page<>(page, limit);
        Map<String, Object> map =commentService.getCommentInfo(pageParam,courseId);
        return Result.ok().data(map);
    }
    @ApiOperation(value = "添加评论")
    @PostMapping("saveUserComment")
    public Result saveUserComment(@RequestBody EduComment comment, HttpServletRequest request) {
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        if(StringUtils.isEmpty(memberId)) {
            return Result.error().code(28004).message("请登录");
        }
        comment.setMemberId(memberId);
        comment.setReplyId("0");
        User ucenterInfo = ucenterClient.getUserInfo(memberId);

        comment.setNickname(ucenterInfo.getNickname());
        comment.setAvatar(ucenterInfo.getAvatar());

        commentService.save(comment);
        return Result.ok();
    }
}

